package cn.lsoft.dao.mongodb;

import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import cn.lsoft.model.SensorData;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
//import com.ketayao.ketacustom.entity.main.User;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.QueryBuilder;
import com.mongodb.WriteConcern;

public class MongoUtils {
	// DBCursor cursor =
	// coll.find(condition).addOption(Bytes.QUERYOPTION_NOTIMEOUT);//设置游标不要超时

	/**
	 * 获取所有数据库实例
	 */
	public void testGetDBS() {
		List<String> dbnames = MongoInit.getMong().getDatabaseNames();
		for (String dbname : dbnames) {
			System.out.println("dbname:" + dbname);
		}
	}

	/**
	 * 删除数据库
	 */
	public void dropDatabase(String dbname) {
		MongoInit.getMong().dropDatabase(dbname);
	}

	/**
	 * 查询所有表名
	 */
	public void getAllCollections() {
		Set<String> colls = MongoInit.getDB().getCollectionNames();
		for (String s : colls) {
			System.out.println("Collections:" + s);
		}
	}

	/**
	 * 删除一个表
	 */
	public void dropCollection(String collection) {
		MongoInit.getColl(collection).drop();
	}

	/**
	 * 添加一条记录
	 */

	public void addData(String dbCollection, String dataID, String dataTime,
			String dataContent, String dataStatus) {
		DBCollection coll = MongoInit.getColl(dbCollection);
		BasicDBObject doc = new BasicDBObject();
		doc.put("id", dataID);
		doc.put("time", dataTime);
		doc.put("data", dataContent);
		doc.put("status", dataStatus);
		coll.insert(doc);
		// 设定write concern，以便操作失败时得到提示
		coll.setWriteConcern(WriteConcern.SAFE);
		findOne(dbCollection);
	}

	/**
	 * 创建索引
	 */
	public void createIndex(String collection) {
		MongoInit.getColl(collection).createIndex(
				new BasicDBObject("index_id", 1));
	}

	/**
	 * 获取索引信息
	 */
	public void getIndexInfo(String dbCollection) {
		List<DBObject> list = MongoInit.getColl(dbCollection).getIndexInfo();
		for (DBObject o : list) {
			System.out.println(o);
		}
	}

	/**
	 * 添加多条记录
	 */
	public void addMultiData() {
		for (int i = 0; i < 100; i++) {
			MongoInit.getColl("wujintao").insert(
					new BasicDBObject().append("i", i));
		}

		List<DBObject> docs = new ArrayList<DBObject>();
		for (int i = 0; i < 50; i++) {
			docs.add(new BasicDBObject().append("i", i));
		}
		MongoInit.getColl("wujintao").insert(docs);
		// 设定write concern，以便操作失败时得到提示
		MongoInit.getColl("wujintao").setWriteConcern(WriteConcern.SAFE);
	}

	/**
	 * 查找第一条记录
	 */
	public void findOne(String dbCollection) {
		DBObject myDoc = MongoInit.getColl(dbCollection).findOne();
		System.out.println(myDoc);
	}

	/**
	 * 获取表中所有记录条数
	 */
	public void count(String dbCollection) {
		System.out.println(MongoInit.getColl(dbCollection).getCount());
		System.out.println(MongoInit.getColl(dbCollection).count());
	}

	/**
	 * 获取查询结果集的记录数
	 */
	public void getCount(String dbCollection, String dataID) {
		DBObject query = new BasicDBObject("id", dataID);
		long count = MongoInit.getColl(dbCollection).count(query);
		System.out.println(count);
	}

	/**
	 * 查询所有结果
	 */
	public void getAllDocuments(String dbCollection) {
		DBCursor cursor = MongoInit.getColl(dbCollection).find();
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	/**
	 * 按照一个条件查询
	 */
	public void queryByConditionOne() {
		BasicDBObject query = new BasicDBObject();
		query.put("name", "MongoDB");
		DBCursor cursor = MongoInit.getColl("wujintao").find(query);

		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public void testBypropertys() {
		BasicDBObject query = new BasicDBObject();
		query = new BasicDBObject();
		query.put("time", new BasicDBObject("$gte", "2014-10-27 00:00:00")
				.append("$lte", "2014-11-01 00:00:00"));
		query.put("id", "12345678");
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryBypropertys(String sname, String startime,
			String endtime, String tableName) {
		// TODO Auto-generated method stub

		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		query.put("time",new BasicDBObject("$gte", startime).append("$lte", endtime));
		query.put("id", sname);
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort);
		List<DBObject> list = cursor.toArray();
	
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
	}

	public void testBystar() {
		BasicDBObject query = new BasicDBObject();
		query.put("time", new BasicDBObject("$gte", "2014-10-28 00:00:00"));
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryBystar(String startime, String tableName) {
		// TODO Auto-generated method stub
		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		// 查询j不等于3,k大于10的结果集
		query.put("time", new BasicDBObject("$gte", startime));
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort);
		List<DBObject> list = cursor.toArray();
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
	}

	public void testByend() {
		BasicDBObject query = new BasicDBObject();
		query.put("time", new BasicDBObject("$lte", "2014-11-01 00:00:00"));
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryByend(String endtime, String tableName) {
		// TODO Auto-generated method stub
		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		// 查询j不等于3,k大于10的结果集
		query.put("time", new BasicDBObject("$lte", endtime));
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort);
		List<DBObject> list = cursor.toArray();
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
	}

	public void testBystarandname() {
		BasicDBObject query = new BasicDBObject();
		query.put("time", new BasicDBObject("$gte", "2014-10-28 00:00:00"));
		query.put("id", "12345678");
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryBystarandname(String sname, String startime,
			String tableName) {
		// TODO Auto-generated method stub
		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		// 查询j不等于3,k大于10的结果集
		query.put("time", new BasicDBObject("$gte", startime));
		query.put("id", sname);
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort);
		List<DBObject> list = cursor.toArray();
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
	}

	public void testBybetween() {
		BasicDBObject query = new BasicDBObject();
		query.put("time", new BasicDBObject("$gte", "2014-10-28 00:00:00")
				.append("$lte", "2014-11-04 00:00:00"));
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryBybetween(String startime, String endtime,
			String tableName) {
		// TODO Auto-generated method stub
		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		// 查询j不等于3,k大于10的结果集
		query.put("time",
				new BasicDBObject("$gte", startime).append("$lte", endtime));
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort);
		List<DBObject> list = cursor.toArray();
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
	}

	public void testByendandname() {
		BasicDBObject query = new BasicDBObject();
		query.put("time", new BasicDBObject("$lte", "2014-11-04 00:00:00"));
		query.put("id", "12345678");
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryByendandname(String sname, String endtime,
			String tableName) {
		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		// 查询j不等于3,k大于10的结果集
		query.put("time", new BasicDBObject("$lte", endtime));
		query.put("id", sname);
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort);
		List<DBObject> list = cursor.toArray();
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
	}

	public void queryBetween() {
		BasicDBObject query = new BasicDBObject();
		query = new BasicDBObject();

		query.put("time", new BasicDBObject("$gte", "2014-10-28 00:00:00")
				.append("$lte", "2014-11-04 00:00:00"));
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public void testByid() {
		BasicDBObject query = new BasicDBObject();
		query.put("id", "12345678");
		DBCursor cursor = MongoInit.getColl("table_admin").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public List<SensorData> queryById(String id, String tableName) {
		BasicDBObject dsort = new BasicDBObject();
		dsort.put("time", -1);
		BasicDBObject query = new BasicDBObject();
		query.put("id", id);
		DBCursor cursor = MongoInit.getColl(tableName)
				.find(query).sort(dsort).limit(100);
		List<DBObject> list = cursor.toArray();
		System.out.println("dao.." + list.toString());
		List<SensorData> sensordata = new ArrayList<SensorData>();
		for (int i = 1; i < list.size(); i++) {
			String sid = (String) list.get(i).get("id");
			String stime = (String) list.get(i).get("time");
			String sdata = (String) list.get(i).get("data");
			String status = (String) list.get(i).get("status");
			String sstatus;
			if (status.equals("0"))
				sstatus = "normal";
			else if (status.equals("1"))
				sstatus = "abnormal";
			else
				sstatus = "illegal";
			SensorData a = new SensorData(sid, stime, sdata, sstatus);
			// System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
			sensordata.add(a);
		}
		return sensordata;
		/*
		 * try { while (cursor.hasNext()) { System.out.println(cursor.next()); }
		 * } finally { cursor.close(); }
		 */
	}

	/*
	 * public void queryByproperty(){ Pattern john =
	 * Pattern.compile("2014-10-29"); BasicDBObject query = new
	 * BasicDBObject("time", john); DBCursor cursor =
	 * MongoInit.getColl("table_admin").find(query); try { while
	 * (cursor.hasNext()) { System.out.println(cursor.next()); } } finally {
	 * cursor.close(); } } public void queryBypropertys(){ Pattern john =
	 * Pattern.compile("2014-10-29"); BasicDBObject query = new
	 * BasicDBObject("time", john); query.put("id", "133"); DBCursor cursor =
	 * MongoInit.getColl("table_admin").find(query); try { while
	 * (cursor.hasNext()) { System.out.println(cursor.next()); } } finally {
	 * cursor.close(); } }
	 */
	/**
	 * AND多条件查询,区间查询
	 */
	public void queryMulti() {
		BasicDBObject query = new BasicDBObject();
		// 查询j不等于3,k大于10的结果集
		query.put("j", new BasicDBObject("$ne", 3));
		query.put("k", new BasicDBObject("$gt", 10));
		DBCursor cursor = MongoInit.getColl("wujintao").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	/**
	 * 区间查询 select * from table where i >50
	 */
	public void queryMulti2() {
		BasicDBObject query = new BasicDBObject();
		query = new BasicDBObject();
		query.put("i", new BasicDBObject("$gt", 50)); // e.g. find all where i >
		DBCursor cursor = MongoInit.getColl("wujintao").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	/**
	 * 区间查询 select * from table where 20 < i <= 30 //比较符 //"$gt"： 大于
	 * //"$gte"：大于等于 //"$lt"： 小于 //"$lte"：小于等于 //"$in"： 包含
	 */
	public void queryMulti3() {
		BasicDBObject query = new BasicDBObject();
		query = new BasicDBObject();

		query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));
		DBCursor cursor = MongoInit.getColl("wujintao").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	/**
	 * 组合in和and select * from test_Table where (a=5 or b=6) and (c=5 or d = 6)
	 */
	public void queryMulti4() {
		BasicDBObject query11 = new BasicDBObject();
		query11.put("a", 1);
		BasicDBObject query12 = new BasicDBObject();
		query12.put("b", 2);
		List<BasicDBObject> orQueryList1 = new ArrayList<BasicDBObject>();
		orQueryList1.add(query11);
		orQueryList1.add(query12);
		BasicDBObject orQuery1 = new BasicDBObject("$or", orQueryList1);

		BasicDBObject query21 = new BasicDBObject();
		query21.put("c", 5);
		BasicDBObject query22 = new BasicDBObject();
		query22.put("d", 6);
		List<BasicDBObject> orQueryList2 = new ArrayList<BasicDBObject>();
		orQueryList2.add(query21);
		orQueryList2.add(query22);
		BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2);

		List<BasicDBObject> orQueryCombinationList = new ArrayList<BasicDBObject>();
		orQueryCombinationList.add(orQuery1);
		orQueryCombinationList.add(orQuery2);

		BasicDBObject finalQuery = new BasicDBObject("$and",
				orQueryCombinationList);
		DBCursor cursor = MongoInit.getColl("wujintao").find(finalQuery);
	}

	/**
	 * IN查询 if i need to query name in (a,b); just use { name : { $in : ['a',
	 * 'b'] } } select * from things where name='a' or name='b'
	 * 
	 * @param coll
	 */
	public void queryIn() {
		BasicDBList values = new BasicDBList();
		values.add("a");
		values.add("b");
		BasicDBObject in = new BasicDBObject("$in", values);
		DBCursor cursor = MongoInit.getColl("wujintao").find(
				new BasicDBObject("name", in));
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	/**
	 * 或查询 select * from table where name = '12' or title = 'p'
	 * 
	 * @param coll
	 */
	public void queryOr() {
		QueryBuilder query = new QueryBuilder();
		query.or(new BasicDBObject("name", 12), new BasicDBObject("title", "p"));
		DBCursor cursor = MongoInit.getColl("wujintao").find(query.get())
				.addSpecial("$returnKey", "");
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	public void customQueryField() throws UnknownHostException {
		Mongo mongo = new Mongo("localhost", 27017);
		DB db = mongo.getDB("zhongsou_ad");
		BasicDBObjectBuilder bulder = new BasicDBObjectBuilder();
		bulder.add("times", 1);
		bulder.add("aid", 1);
		DBCursor cusor = db.getCollection("ad_union_ad_c_1").find(
				new BasicDBObject(), bulder.get());
		for (DBObject dbObject : cusor) {
			System.out.println(dbObject);
		}
	}

	public void mapReduce() throws UnknownHostException {
		Mongo mongo = new Mongo("localhost", 27017);
		DB db = mongo.getDB("zhongsou_ad");
		/***
		 * book1 = {name : "Understanding JAVA", pages : 100} book2 = {name :
		 * "Understanding JSON", pages : 200} db.books.save(book1)
		 * db.books.save(book2) book = {name : "Understanding XML", pages : 300}
		 * db.books.save(book) book = {name : "Understanding Web Services",
		 * pages : 400} db.books.save(book) book = {name :
		 * "Understanding Axis2", pages : 150} db.books.save(book)
		 * 
		 * var map = function() { var category; if ( this.pages >= 250 )
		 * category = 'Big Books'; else category = "Small Books"; emit(category,
		 * {name: this.name}); }; var reduce = function(key, values) { var sum =
		 * 0; values.forEach(function(doc) { sum += 1; }); return {books: sum};
		 * }; var count = db.books.mapReduce(map, reduce, {out:
		 * "book_results"});
		 */
		try {

			DBCollection books = db.getCollection("books");

			BasicDBObject book = new BasicDBObject();
			book.put("name", "Understanding JAVA");
			book.put("pages", 100);
			books.insert(book);

			book = new BasicDBObject();
			book.put("name", "Understanding JSON");
			book.put("pages", 200);
			books.insert(book);

			book = new BasicDBObject();
			book.put("name", "Understanding XML");
			book.put("pages", 300);
			books.insert(book);

			book = new BasicDBObject();
			book.put("name", "Understanding Web Services");
			book.put("pages", 400);
			books.insert(book);

			book = new BasicDBObject();
			book.put("name", "Understanding Axis2");
			book.put("pages", 150);
			books.insert(book);

			String map = "function() { " + "var category; "
					+ "if ( this.pages >= 250 ) " + "category = 'Big Books'; "
					+ "else " + "category = 'Small Books'; "
					+ "emit(category, {name: this.name});}";

			String reduce = "function(key, values) { " + "var sum = 0; "
					+ "values.forEach(function(doc) { " + "sum += 1; " + "}); "
					+ "return {books: sum};} ";

			MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
					null, MapReduceCommand.OutputType.INLINE, null);

			MapReduceOutput out = books.mapReduce(cmd);

			for (DBObject o : out.results()) {
				System.out.println(o.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void GroupByManyField() throws UnknownHostException {
		// 此方法没有运行成功
		Mongo mongo = new Mongo("localhost", 27017);
		DB db = mongo.getDB("libary");
		DBCollection books = db.getCollection("books");
		BasicDBObject groupKeys = new BasicDBObject();
		groupKeys.put("total", new BasicDBObject("$sum", "pages"));

		BasicDBObject condition = new BasicDBObject();
		condition.append("pages", new BasicDBObject().put("$gt", 0));

		String reduce = "function(key, values) { " + "var sum = 0; "
				+ "values.forEach(function(doc) { " + "sum += 1; " + "}); "
				+ "return {books: sum};} ";
		/**
		 * BasicDBList basicDBList =
		 * (BasicDBList)db.getCollection("mongodb中集合编码或者编码") .group(DBObject
		 * key, --分组字段，即group by的字段 DBObject cond, --查询中where条件 DBObject
		 * initial, --初始化各字段的值 String reduce, --每个分组都需要执行的Function String finial
		 * --终结Funciton对结果进行最终的处理
		 */
		DBObject obj = books.group(groupKeys, condition, new BasicDBObject(),
				reduce);
		System.out.println(obj);

		AggregationOutput ouput = books.aggregate(new BasicDBObject("$group",
				groupKeys));
		System.out.println(ouput.getCommandResult());
		System.out.println(books.find(new BasicDBObject("$group", groupKeys)));
	}

	/**
	 * 分页查询
	 */
	public void pageQuery() {
		DBCursor cursor = MongoInit.getColl("wujintao").find().skip(0)
				.limit(10);
		while (cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}

	/**
	 * 模糊查询
	 */
	public void likeQuery() {
		Pattern john = Pattern.compile("joh?n");
		BasicDBObject query = new BasicDBObject("name", john);

		// finds all people with "name" matching /joh?n/i
		DBCursor cursor = MongoInit.getColl("wujintao").find(query);
	}

	/**
	 * 条件删除
	 */
	public void delete(String dbCollection, String dataID) {
		BasicDBObject query = new BasicDBObject();
		query.put("id", dataID);
		// 找到并且删除，并返回删除的对象
		DBObject removeObj = MongoInit.getColl(dbCollection).findAndRemove(
				query);
		System.out.println(removeObj);
	}

	/**
	 * 更新
	 */
	public void update() {
		BasicDBObject query = new BasicDBObject();
		query.put("name", "liu");
		DBObject stuFound = MongoInit.getColl("wujintao").findOne(query);
		stuFound.put("name", stuFound.get("name") + "update_1");
		MongoInit.getColl("wujintao").update(query, stuFound);
	}

	public void testGpsData() {
		try {
			// 13862082455:117.13172:029.77659:131.2580
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateNowStr = sdf.format(d);
			System.out.println(dateNowStr);

			DBCollection coll = MongoInit.getColl("table_admin");
			BasicDBObject doc = new BasicDBObject();
			doc.put("id", "12345678");
			doc.put("time", dateNowStr);
			doc.put("data", "117.13172:029.77659:131.2580");
			doc.put("status", "0");
			coll.insert(doc);
			// 设定write concern，以便操作失败时得到提示
			coll.setWriteConcern(WriteConcern.SAFE);

			BasicDBObject dsort = new BasicDBObject();
			dsort.put("time", -1);

			BasicDBObject searchDevice = new BasicDBObject();
			searchDevice.put("id", "12345678");
			DBCursor cursor = coll.find(searchDevice).sort(dsort);

			List<DBObject> list = cursor.toArray();
			// ArrayList<SensorData> listSensorData=new
			// ArrayList<SensorData>(list.size());

			ObjectMapper mapper = new ObjectMapper();
			String jsonfromList = null;
			try {
				// list转json
				jsonfromList = mapper.writeValueAsString(list);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println(jsonfromList);
			// for(int i=0;i<list.size();i++)
			// {
			// System.out.println((String) list.get(i).get("time"));
			// }

			// System.out.println(list.size()+","+listSensorData);//list的长度
			// System.out.println(cursor.count());//计算结果的数量，类似于(mysql
			// count()函数),不受limit的影响

			// while (cursor.hasNext()) {
			// System.out.println(cursor.next());
			// }
			System.out.println("The Search Query has Executed!");
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String dbname = "wlwdb";
		String dbCollection = "table_admin";
		String dbCollection1 = "15257102317";
		String dataID = "12345678";
		MongoUtils tb = new MongoUtils();
		
		tb.testBybetween();
		// tb.queryBetween();
		// tb.queryByproperty();
		// tb.queryBypropertys();
		// tb.testGetDBS();//查询所有数据库
		// tb.getAllCollections();//查询所有表

		// tb.findOne(dbCollection);//查找第一条记录
		// tb.testGpsData();
		// tb.getAllDocuments(dbCollection);//获取制定表明的所有数据
		// tb.dropDatabase(dbname);//删除指定数据库
		// tb.delete(dbCollection,dataID);//删除制定表明和设备ID的数据
		// tb.getAllCollections();
		// tb.dropCollection(dbCollection);// 删除一个表

		// tb.createIndex(dbCollection1);//创建索引
		// tb.getIndexInfo(dbCollection1);//获得表中所有索引
	}

}
